package org.keyyond.ms.commonservice.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.keyyond.ms.commonservice.common.ResultInfo;
import org.keyyond.ms.commonservice.model.SDBDRole;
import org.keyyond.ms.commonservice.service.SDBDRoleService;
import org.keyyond.ms.commonservice.util.CommonBeanUtils;
import org.keyyond.ms.core.model.MsCommonMessageLogVo;
import org.keyyond.ms.core.util.CommonBeanUtil;
import org.keyyond.ms.core.util.Constant;
import org.keyyond.ms.core.util.ConstantResult;
import org.keyyond.ms.core.util.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.Page;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

@RestController
@RequestMapping("/sdbdRole")
@RefreshScope
public class SDBDRoleController {

	private static Logger logger = Logger.getLogger(SDBDRoleController.class);

	@Autowired
	private CommonBeanUtils beanUtils;

	@Autowired
	private SDBDRoleService roleService;

	@RequestMapping(value = "/selectRolesByRoleIds", method = RequestMethod.POST)
	public String selectRolesByRoleIds(HttpServletRequest req) {
		MsCommonMessageLogVo logVo = new MsCommonMessageLogVo();
		String traceId = req.getHeader(Constant.CONST_REQUEST_HEADER_TRACEID);
		logVo.setTraceid(traceId);
		logVo.setLogtype(org.keyyond.ms.commonservice.util.Constant.CONST_LOG_TYPE_COMMON_SERVICE);
		logVo.setSendtype(org.keyyond.ms.commonservice.util.Constant.CONST_SEND_TYPE_COMMON_SERVICE_FAILE);
		logVo.setContent(JsonUtil.toJson(req.getParameterMap()));
		JSONArray dataArray = new JSONArray();
		try {
			String param = req.getParameter(Constant.PARAM);
			JSONObject paramJson = JSONObject.fromObject(param);
			if (null == paramJson) {
				logVo.setReturnmessage(ConstantResult.RESULT_ERRMSG_PARAM);
				return CommonBeanUtil.returnResult(false, ConstantResult.RESULT_ERRMSG_PARAM);
			}
			JSONArray paramArray = paramJson.getJSONArray(Constant.DATA);
			if (null != paramArray && paramArray.size() > 0) {
				SDBDRole role = JsonUtil.readJson2Bean(paramArray.get(0).toString(), SDBDRole.class);
				List<SDBDRole> roleList = roleService.selectRolesByRoleIds(role);
				for (SDBDRole r : roleList) {
					dataArray.add(JsonUtil.beanToJson(r));
				}
				logVo.setSendtype(org.keyyond.ms.commonservice.util.Constant.CONST_SEND_TYPE_COMMON_SERVICE_SUCCESS);
				logVo.setReturnmessage(dataArray.toString());
			} else {
				logVo.setReturnmessage(ConstantResult.RESULT_ERRMSG_PARAM);
				return CommonBeanUtil.returnResult(false, ConstantResult.RESULT_ERRMSG_PARAM);
			}

			beanUtils.addMessageLog(logVo);
			return CommonBeanUtil.returnResult(true, dataArray);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return Constant.FALL_BACK_INFO;
		}
	}
	
	@RequestMapping(value = "/getAllFDFBRolesCount", method = RequestMethod.POST)
	public String getAllFDFBRolesCount(HttpServletRequest req) {
		MsCommonMessageLogVo logVo = new MsCommonMessageLogVo();
		String traceId = req.getHeader(Constant.CONST_REQUEST_HEADER_TRACEID);
		logVo.setTraceid(traceId);
		logVo.setLogtype(org.keyyond.ms.commonservice.util.Constant.CONST_LOG_TYPE_COMMON_SERVICE);
		logVo.setSendtype(org.keyyond.ms.commonservice.util.Constant.CONST_SEND_TYPE_COMMON_SERVICE_FAILE);
		logVo.setContent(JsonUtil.toJson(req.getParameterMap()));
		JSONArray dataArray = new JSONArray();
		try {
			String param = req.getParameter(Constant.PARAM);
			JSONObject paramJson = JSONObject.fromObject(param);
			if (null == paramJson) {
				logVo.setReturnmessage(ConstantResult.RESULT_ERRMSG_PARAM);
				return CommonBeanUtil.returnResult(false, ConstantResult.RESULT_ERRMSG_PARAM);
			}
			JSONArray paramArray = paramJson.getJSONArray(Constant.DATA);
			if (null != paramArray && paramArray.size() > 0) {
				SDBDRole role = JsonUtil.readJson2Bean(paramArray.get(0).toString(), SDBDRole.class);
				int count = roleService.getAllFDFBRolesCount(role);
				dataArray.add(count);
				logVo.setSendtype(org.keyyond.ms.commonservice.util.Constant.CONST_SEND_TYPE_COMMON_SERVICE_SUCCESS);
				logVo.setReturnmessage(dataArray.toString());
			} else {
				logVo.setReturnmessage(ConstantResult.RESULT_ERRMSG_PARAM);
				return CommonBeanUtil.returnResult(false, ConstantResult.RESULT_ERRMSG_PARAM);
			}

			beanUtils.addMessageLog(logVo);
			return CommonBeanUtil.returnResult(true, dataArray);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return Constant.FALL_BACK_INFO;
		}
	}
	
	@RequestMapping(value = "/addFDFBRole", method = RequestMethod.POST)
	public String addFDFBRole(HttpServletRequest req) {
		MsCommonMessageLogVo logVo = new MsCommonMessageLogVo();
		String traceId = req.getHeader(Constant.CONST_REQUEST_HEADER_TRACEID);
		logVo.setTraceid(traceId);
		logVo.setLogtype(org.keyyond.ms.commonservice.util.Constant.CONST_LOG_TYPE_COMMON_SERVICE);
		logVo.setSendtype(org.keyyond.ms.commonservice.util.Constant.CONST_SEND_TYPE_COMMON_SERVICE_FAILE);
		logVo.setContent(JsonUtil.toJson(req.getParameterMap()));
		JSONArray dataArray = new JSONArray();
		try {
			String param = req.getParameter(Constant.PARAM);
			JSONObject paramJson = JSONObject.fromObject(param);
			if (null == paramJson) {
				logVo.setReturnmessage(ConstantResult.RESULT_ERRMSG_PARAM);
				return CommonBeanUtil.returnResult(false, ConstantResult.RESULT_ERRMSG_PARAM);
			}
			JSONArray paramArray = paramJson.getJSONArray(Constant.DATA);
			if (null != paramArray && paramArray.size() > 0) {
				SDBDRole role = JsonUtil.jsonTBean(paramArray.get(0).toString(), SDBDRole.class);
				int count = roleService.addFDFBRole(role);
				dataArray.add(count);
				logVo.setSendtype(org.keyyond.ms.commonservice.util.Constant.CONST_SEND_TYPE_COMMON_SERVICE_SUCCESS);
				logVo.setReturnmessage(dataArray.toString());
			} else {
				logVo.setReturnmessage(ConstantResult.RESULT_ERRMSG_PARAM);
				return CommonBeanUtil.returnResult(false, ConstantResult.RESULT_ERRMSG_PARAM);
			}

			beanUtils.addMessageLog(logVo);
			return CommonBeanUtil.returnResult(true, dataArray);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return Constant.FALL_BACK_INFO;
		}
	}
	
	@RequestMapping(value = "/updateRoleDynamic", method = RequestMethod.POST)
	public String updateRoleDynamic(HttpServletRequest req) {
		MsCommonMessageLogVo logVo = new MsCommonMessageLogVo();
		String traceId = req.getHeader(Constant.CONST_REQUEST_HEADER_TRACEID);
		logVo.setTraceid(traceId);
		logVo.setLogtype(org.keyyond.ms.commonservice.util.Constant.CONST_LOG_TYPE_COMMON_SERVICE);
		logVo.setSendtype(org.keyyond.ms.commonservice.util.Constant.CONST_SEND_TYPE_COMMON_SERVICE_FAILE);
		logVo.setContent(JsonUtil.toJson(req.getParameterMap()));
		JSONArray dataArray = new JSONArray();
		try {
			String param = req.getParameter(Constant.PARAM);
			JSONObject paramJson = JSONObject.fromObject(param);
			if (null == paramJson) {
				logVo.setReturnmessage(ConstantResult.RESULT_ERRMSG_PARAM);
				return CommonBeanUtil.returnResult(false, ConstantResult.RESULT_ERRMSG_PARAM);
			}
			JSONArray paramArray = paramJson.getJSONArray(Constant.DATA);
			if (null != paramArray && paramArray.size() > 0) {
				SDBDRole role = JsonUtil.jsonTBean(paramArray.get(0).toString(), SDBDRole.class);
				int count = roleService.updateRoleDynamic(role);
				dataArray.add(count);
				logVo.setSendtype(org.keyyond.ms.commonservice.util.Constant.CONST_SEND_TYPE_COMMON_SERVICE_SUCCESS);
				logVo.setReturnmessage(dataArray.toString());
			} else {
				logVo.setReturnmessage(ConstantResult.RESULT_ERRMSG_PARAM);
				return CommonBeanUtil.returnResult(false, ConstantResult.RESULT_ERRMSG_PARAM);
			}

			beanUtils.addMessageLog(logVo);
			return CommonBeanUtil.returnResult(true, dataArray);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return Constant.FALL_BACK_INFO;
		}
	}
	
	@RequestMapping(value = "/deleteUserBySeqno", method = RequestMethod.POST)
	public String deleteUserBySeqno(HttpServletRequest req) {
		MsCommonMessageLogVo logVo = new MsCommonMessageLogVo();
		String traceId = req.getHeader(Constant.CONST_REQUEST_HEADER_TRACEID);
		logVo.setTraceid(traceId);
		logVo.setLogtype(org.keyyond.ms.commonservice.util.Constant.CONST_LOG_TYPE_COMMON_SERVICE);
		logVo.setSendtype(org.keyyond.ms.commonservice.util.Constant.CONST_SEND_TYPE_COMMON_SERVICE_FAILE);
		logVo.setContent(JsonUtil.toJson(req.getParameterMap()));
		JSONArray dataArray = new JSONArray();
		try {
			String param = req.getParameter(Constant.PARAM);
			JSONObject paramJson = JSONObject.fromObject(param);
			if (null == paramJson) {
				logVo.setReturnmessage(ConstantResult.RESULT_ERRMSG_PARAM);
				return CommonBeanUtil.returnResult(false, ConstantResult.RESULT_ERRMSG_PARAM);
			}
			JSONArray paramArray = paramJson.getJSONArray(Constant.DATA);
			if (null != paramArray && paramArray.size() > 0) {
				String seqno = paramArray.get(0).toString();
				int count = roleService.deleteUserBySeqno(seqno);
				dataArray.add(count);
				logVo.setSendtype(org.keyyond.ms.commonservice.util.Constant.CONST_SEND_TYPE_COMMON_SERVICE_SUCCESS);
				logVo.setReturnmessage(dataArray.toString());
			} else {
				logVo.setReturnmessage(ConstantResult.RESULT_ERRMSG_PARAM);
				return CommonBeanUtil.returnResult(false, ConstantResult.RESULT_ERRMSG_PARAM);
			}

			beanUtils.addMessageLog(logVo);
			return CommonBeanUtil.returnResult(true, dataArray);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return Constant.FALL_BACK_INFO;
		}
	}

	@RequestMapping(value = "/selectAll", method = RequestMethod.POST)
	public String selectAll(HttpServletRequest req) {
		MsCommonMessageLogVo logVo = new MsCommonMessageLogVo();
		String traceId = req.getHeader(Constant.CONST_REQUEST_HEADER_TRACEID);
		logVo.setTraceid(traceId);
		logVo.setLogtype(org.keyyond.ms.commonservice.util.Constant.CONST_LOG_TYPE_COMMON_SERVICE);
		logVo.setSendtype(org.keyyond.ms.commonservice.util.Constant.CONST_SEND_TYPE_COMMON_SERVICE_FAILE);
		logVo.setContent(JsonUtil.toJson(req.getParameterMap()));
		JSONArray dataArray = new JSONArray();
		try {
			List<SDBDRole> roleList = roleService.selectAll();
			for (SDBDRole r : roleList) {
				dataArray.add(JsonUtil.beanToJson(r));
			}
			logVo.setSendtype(org.keyyond.ms.commonservice.util.Constant.CONST_SEND_TYPE_COMMON_SERVICE_SUCCESS);
			logVo.setReturnmessage(dataArray.toString());
			beanUtils.addMessageLog(logVo);
			return CommonBeanUtil.returnResult(true, dataArray);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return Constant.FALL_BACK_INFO;
		}
	}
	
	@RequestMapping(value = "/selectAllByPage", method = RequestMethod.POST)
	public String selectAllByPage(HttpServletRequest req) {
		MsCommonMessageLogVo logVo = new MsCommonMessageLogVo();
		String traceId = req.getHeader(Constant.CONST_REQUEST_HEADER_TRACEID);
		logVo.setTraceid(traceId);
		logVo.setLogtype(org.keyyond.ms.commonservice.util.Constant.CONST_LOG_TYPE_COMMON_SERVICE);
		logVo.setSendtype(org.keyyond.ms.commonservice.util.Constant.CONST_SEND_TYPE_COMMON_SERVICE_FAILE);
		logVo.setContent(JsonUtil.toJson(req.getParameterMap()));
		JSONArray dataArray = new JSONArray();
		try {
			String param = req.getParameter(Constant.PARAM);
			JSONObject paramJson = JSONObject.fromObject(param);
			if(null == paramJson) {
				logVo.setReturnmessage(ConstantResult.RESULT_ERRMSG_PARAM);
				return CommonBeanUtil.returnResult(false, ConstantResult.RESULT_ERRMSG_PARAM);
			}
			JSONArray paramArray = paramJson.getJSONArray(Constant.DATA);
			if(null != paramArray && paramArray.size()>0) {
				JSONObject dataJson = new JSONObject();
				dataJson = JSONObject.fromObject(paramArray.get(0));
				Page<SDBDRole> rolePage = roleService.selectAllByPage(dataJson);
				ResultInfo<SDBDRole> roleInfo = new ResultInfo<SDBDRole>(rolePage);
				JSONArray roleArr = new JSONArray();
				for(SDBDRole r : roleInfo.getList()) {
					roleArr.add(JsonUtil.beanToJson(r));
				}
				JSONObject resJson = new JSONObject();
				resJson.put("role", roleArr);
				resJson.put(Constant.PAGE_TOTALCOUNT, roleInfo.getTotalItemCount());
				dataArray.add(resJson);
				logVo.setSendtype(org.keyyond.ms.commonservice.util.Constant.CONST_SEND_TYPE_COMMON_SERVICE_SUCCESS);
				logVo.setReturnmessage(dataArray.toString());
			}else {
				logVo.setReturnmessage(ConstantResult.RESULT_ERRMSG_PARAM);
				return CommonBeanUtil.returnResult(false, ConstantResult.RESULT_ERRMSG_PARAM);
			}
			
			beanUtils.addMessageLog(logVo);
			return CommonBeanUtil.returnResult(true, dataArray);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return Constant.FALL_BACK_INFO;
		}
	}
}
